"""
Case Type   : 资源池化
Case Name   : 资源池化场景下, 主机设置磁盘检测阈值
Create At   : 2024-11-26
Owner       : jiangyan
Description :
    1、查看集群状态 cm_ctl query -Cv
    2、修改磁盘阈值参数datastorage_threshold_value_check为1并加载参数
    3、查询集群状态 cm_ctl query -Cv
    4、修改磁盘阈值参数datastorage_threshold_value_check为85并加载参数
    5、查看集群状态 cm_ctl query -Cv
Expect      :
    1、集群状态正常，数据库节点状态正常
    2、参数设置成功
    3、数据库节点状态进入readonly
    4、参数设置成功
    5、集群状态正常，数据库节点状态正常
History     :
    1、2024-11-26 创建
"""

import os
import time
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')

class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()

    def test_shared_storage(self):
        text = '-----step1:查看集群状态; expect:集群状态正常-----'
        self.log.info(text)

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '-----step2:设置参数cm_ctl set --param --server -k datastorage_threshold_value_check=1; expect:执行成功-----'
        self.log.info(text)

        status = Primary_SH.exec_cm_ctl(mode='set', param='--param --server -k datastorage_threshold_value_check=1')
        self.log.info(status)
        self.assertIn('success', status, '执行失败' + text)

        text = '-----step3:加载参数cm_ctl reload --param --server; expect:参数设置成功-----'
        self.log.info(text)

        status = Primary_SH.exec_cm_ctl(mode='reload', param='--param --server')
        self.log.info(status)
        self.assertIn('success', status, '执行失败' + text)
        
        status = Primary_SH.exec_cm_ctl(mode='list', param='--param --server | grep datastorage_threshold_value_check')
        self.log.info(status)
        self.assertIn('datastorage_threshold_value_check = 1', status, '执行失败' + text)

        time.sleep(60)

        text = '-----step4:查看集群状态; expect:节点状态进入readonly;-----'
        self.log.info(text)

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('P Primary ReadOnly',
                      status.splitlines()[-1].split('|')[0].split('6001')[-1],
                      '执行失败' + text)
        self.assertIn('S Standby ReadOnly',
                      status.splitlines()[-1].split('|')[1].split('6002')[-1],
                      '执行失败' + text)
        self.assertIn('S Standby ReadOnly',
                      status.splitlines()[-1].split('|')[2].split('6003')[-1],
                      '执行失败' + text)
    
    def tearDown(self):
        text = '-----step5:设置参数cm_ctl set --param --server -k datastorage_threshold_value_check=85; expect:执行成功-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        status = Primary_SH.check_cluster_core_status(status, wait_times=600)
        self.log.info(status)

        status = Primary_SH.exec_cm_ctl(mode='set', param='--param --server -k datastorage_threshold_value_check=85')
        self.log.info(status)
        self.assertIn('success', status, '执行失败' + text)
        
        text = '-----step6:加载参数cm_ctl reload --param --server; expect:参数设置成功-----'
        self.log.info(text)

        status = Primary_SH.exec_cm_ctl(mode='reload', param='--param --server')
        self.log.info(status)
        self.assertIn('success', status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='list', param='--param --server | grep datastorage_threshold_value_check')
        self.log.info(status)
        self.assertIn('datastorage_threshold_value_check = 85', status, '执行失败' + text)

        time.sleep(60)

        text = '-----step7:查看集群状态; expect:集群状态正常-----'
        self.log.info(text)

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')